package com.flyox.game.militarychess.service;

import com.flyox.game.militarychess.CONSTS;
import com.flyox.game.militarychess.bean.ChessMan;
import com.flyox.game.militarychess.bean.Position;
import com.flyox.game.militarychess.bean.ChessMan.NAME;
import com.flyox.game.militarychess.bean.ChessSeat.SEAT_NAME;

public class CommonLayoutService {
	//private final Logger logger = LoggerFactory.getLogger(getClass());

	public int[][] getPosProperties() {
		int[][] posPropertie = new int[13][20];// 全局布局属性
		for (int i = 0; i < 20; i++) {
			posPropertie[0][i] = CONSTS.POS_END_LINE;
			posPropertie[12][i] = CONSTS.POS_END_LINE;
		}
		for (int i = 0; i < 20; i++) {
			posPropertie[1][i] = CONSTS.POS_RAINWAY | CONSTS.POS_END_LINE2;
			posPropertie[5][i] = CONSTS.POS_RAINWAY | CONSTS.POS_FIRST_LINE;
			posPropertie[6][i] = CONSTS.POS_RAINWAY | CONSTS.POS_EMPTY_LINE;
			posPropertie[7][i] = CONSTS.POS_RAINWAY | CONSTS.POS_FIRST_LINE;
			posPropertie[11][i] = CONSTS.POS_RAINWAY | CONSTS.POS_END_LINE2;
		}
		for (int i = 0; i < 4; i++) {
			posPropertie[0][i * 5 + 1] = CONSTS.POS_SUPREME_HEADQUARTER | CONSTS.POS_END_LINE;
			posPropertie[0][i * 5 + 3] = CONSTS.POS_SUPREME_HEADQUARTER | CONSTS.POS_END_LINE;
			posPropertie[6][i * 5 + 1] = CONSTS.POS_EMPTY;
			posPropertie[6][i * 5 + 3] = CONSTS.POS_EMPTY;
			posPropertie[12][i * 5 + 1] = CONSTS.POS_SUPREME_HEADQUARTER | CONSTS.POS_END_LINE;
			posPropertie[12][i * 5 + 3] = CONSTS.POS_SUPREME_HEADQUARTER | CONSTS.POS_END_LINE;
		}
		for (int j = 0; j < 2; j++) {
			//
			for (int i = 0; i < 4; i++) {
				posPropertie[6 * j + 2][i * 5 + 0] = CONSTS.POS_RAINWAY;
				posPropertie[6 * j + 2][i * 5 + 1] = CONSTS.POS_FIELD_HEADQUARTER;
				posPropertie[6 * j + 2][i * 5 + 2] = CONSTS.POS_NORMAL;
				posPropertie[6 * j + 2][i * 5 + 3] = CONSTS.POS_FIELD_HEADQUARTER;
				posPropertie[6 * j + 2][i * 5 + 4] = CONSTS.POS_RAINWAY;

				posPropertie[6 * j + 3][i * 5 + 0] = CONSTS.POS_RAINWAY;
				posPropertie[6 * j + 3][i * 5 + 1] = CONSTS.POS_NORMAL;
				posPropertie[6 * j + 3][i * 5 + 2] = CONSTS.POS_FIELD_HEADQUARTER;
				posPropertie[6 * j + 3][i * 5 + 3] = CONSTS.POS_NORMAL;
				posPropertie[6 * j + 3][i * 5 + 4] = CONSTS.POS_RAINWAY;

				posPropertie[6 * j + 4][i * 5 + 0] = CONSTS.POS_RAINWAY;
				posPropertie[6 * j + 4][i * 5 + 1] = CONSTS.POS_FIELD_HEADQUARTER;
				posPropertie[6 * j + 4][i * 5 + 2] = CONSTS.POS_NORMAL;
				posPropertie[6 * j + 4][i * 5 + 3] = CONSTS.POS_FIELD_HEADQUARTER;
				posPropertie[6 * j + 4][i * 5 + 4] = CONSTS.POS_RAINWAY;
			}
		}
		return posPropertie;
	}

	public ChessMan[][] getDefaultPlayerLayout(SEAT_NAME sn) {

		// ChessMan[][] defaultL=getDefaultLayout();
		if (SEAT_NAME.S1 == sn) {
			return getDefaultLayout(sn, 0, 0);
		} else if (SEAT_NAME.S2 == sn) {
			return getDefaultLayout(sn, 0, 5);
		} else if (SEAT_NAME.S3 == sn) {
			return getDefaultLayout(sn, 0, 10);
		} else if (SEAT_NAME.S4 == sn) {
			return getDefaultLayout(sn, 0, 15);
		} else if (SEAT_NAME.S5 == sn) {
			return getDefaultLayout(sn, 12, 0);
		} else if (SEAT_NAME.S6 == sn) {
			return getDefaultLayout(sn, 12, 5);
		} else if (SEAT_NAME.S7 == sn) {
			return getDefaultLayout(sn, 12, 10);
		} else if (SEAT_NAME.S8 == sn) {
			return getDefaultLayout(sn, 12, 15);
		} else {
			return new ChessMan[13][20];
		}
	}

	/**
	 * 这是默认S1的布局
	 * 
	 * @return
	 */
	private ChessMan[][] getDefaultLayout(SEAT_NAME sn, int row, int col) {
		// ChessMan[][] layout=new ChessMan[13][20];//全局布局

		ChessMan[][] layout = new ChessMan[13][20];
		layout[Math.abs(row - 0)][0 + col] = new ChessMan(NAME.LIANZHANG, sn, new Position(Math.abs(row - 0), 0 + col));
		layout[Math.abs(row - 0)][1 + col] = new ChessMan(NAME.PAIZHANG, sn, new Position(Math.abs(row - 0), 1 + col));
		layout[Math.abs(row - 0)][2 + col] = new ChessMan(NAME.DILEI, sn, new Position(Math.abs(row - 0), 2 + col));
		layout[Math.abs(row - 0)][3 + col] = new ChessMan(NAME.JUNQI, sn, new Position(Math.abs(row - 0), 3 + col));
		layout[Math.abs(row - 0)][4 + col] = new ChessMan(NAME.DILEI, sn, new Position(Math.abs(row - 0), 4 + col));

		layout[Math.abs(row - 1)][0 + col] = new ChessMan(NAME.TUANZHANG, sn, new Position(Math.abs(row - 1), 0 + col));
		layout[Math.abs(row - 1)][1 + col] = new ChessMan(NAME.GONGBING, sn, new Position(Math.abs(row - 1), 1 + col));
		layout[Math.abs(row - 1)][2 + col] = new ChessMan(NAME.LVZHANG, sn, new Position(Math.abs(row - 1), 2 + col));
		layout[Math.abs(row - 1)][3 + col] = new ChessMan(NAME.DILEI, sn, new Position(Math.abs(row - 1), 3 + col));
		layout[Math.abs(row - 1)][4 + col] = new ChessMan(NAME.SHIZHANG, sn, new Position(Math.abs(row - 1), 4 + col));

		layout[Math.abs(row - 2)][0 + col] = new ChessMan(NAME.PAIZHANG, sn, new Position(Math.abs(row - 2), 0 + col));
		layout[Math.abs(row - 2)][1 + col] = new ChessMan(NAME.EMPTY, sn, new Position(Math.abs(row - 2), 1 + col));
		layout[Math.abs(row - 2)][2 + col] = new ChessMan(NAME.YINGZHANG, sn, new Position(Math.abs(row - 2), 2 + col));
		layout[Math.abs(row - 2)][3 + col] = new ChessMan(NAME.EMPTY, sn, new Position(Math.abs(row - 2), 3 + col));
		layout[Math.abs(row - 2)][4 + col] = new ChessMan(NAME.ZHANGDAN, sn, new Position(Math.abs(row - 2), 4 + col));

		layout[Math.abs(row - 3)][0 + col] = new ChessMan(NAME.LVZHANG, sn, new Position(Math.abs(row - 3), 0 + col));
		layout[Math.abs(row - 3)][1 + col] = new ChessMan(NAME.GONGBING, sn, new Position(Math.abs(row - 3), 1 + col));
		layout[Math.abs(row - 3)][2 + col] = new ChessMan(NAME.EMPTY, sn, new Position(Math.abs(row - 3), 2 + col));
		layout[Math.abs(row - 3)][3 + col] = new ChessMan(NAME.LIANZHANG, sn, new Position(Math.abs(row - 3), 3 + col));
		layout[Math.abs(row - 3)][4 + col] = new ChessMan(NAME.SHIZHANG, sn, new Position(Math.abs(row - 3), 4 + col));

		layout[Math.abs(row - 4)][0 + col] = new ChessMan(NAME.YINGZHANG, sn, new Position(Math.abs(row - 4), 0 + col));
		layout[Math.abs(row - 4)][1 + col] = new ChessMan(NAME.EMPTY, sn, new Position(Math.abs(row - 4), 1 + col));
		layout[Math.abs(row - 4)][2 + col] = new ChessMan(NAME.ZHANGDAN, sn, new Position(Math.abs(row - 4), 2 + col));
		layout[Math.abs(row - 4)][3 + col] = new ChessMan(NAME.EMPTY, sn, new Position(Math.abs(row - 4), 3 + col));
		layout[Math.abs(row - 4)][4 + col] = new ChessMan(NAME.JUNZHANG, sn, new Position(Math.abs(row - 4), 4 + col));

		layout[Math.abs(row - 5)][0 + col] = new ChessMan(NAME.TUANZHANG, sn, new Position(Math.abs(row - 5), 0 + col));
		layout[Math.abs(row - 5)][1 + col] = new ChessMan(NAME.LIANZHANG, sn, new Position(Math.abs(row - 5), 1 + col));
		layout[Math.abs(row - 5)][2 + col] = new ChessMan(NAME.GONGBING, sn, new Position(Math.abs(row - 5), 2 + col));
		layout[Math.abs(row - 5)][3 + col] = new ChessMan(NAME.PAIZHANG, sn, new Position(Math.abs(row - 5), 3 + col));
		layout[Math.abs(row - 5)][4 + col] = new ChessMan(NAME.SILING, sn, new Position(Math.abs(row - 5), 4 + col));

		//更改视角
		if(CONSTS.SYS_CFG_CHESS_CONVERT){
			if(sn.equals(SEAT_NAME.S1)||sn.equals(SEAT_NAME.S2)||sn.equals(SEAT_NAME.S3)||sn.equals(SEAT_NAME.S4)){
				changeVisualAngle(layout);
			}
		}
		return layout;
	}
	public void copyLayout(ChessMan[][] source, ChessMan[][] target,SEAT_NAME sn ) {
		if (CONSTS.SYS_CFG_CHESS_CONVERT&&(sn.equals(SEAT_NAME.S1) || sn.equals(SEAT_NAME.S2) || sn.equals(SEAT_NAME.S3) || sn.equals(SEAT_NAME.S4))) {
			copyPlayerLayoutConvert(source, target, sn);
		} else {
			copyPlayerLayout(source, target, sn);
		}
	}
	/**
	 * 在服务端保存客户布局 只替换单个team 只能初始化自己的布局13×20
	 * 
	 * @param pp
	 */
	public void copyPlayerLayout(ChessMan[][] source, ChessMan[][] target, SEAT_NAME sn) {
		if (sn == null || source == null) {
			System.out.println("parameter error");
			return;
		}
		if (source.length != 13) {
			System.out.println("source.length!=13");
			return;
		} else if (SEAT_NAME.S1 == sn) {
			init(0, 0, source, target);
		} else if (SEAT_NAME.S2 == sn) {
			init(0, 5, source, target);
		} else if (SEAT_NAME.S3 == sn) {
			init(0, 10, source, target);
		} else if (SEAT_NAME.S4 == sn) {
			init(0, 15, source, target);
		} else if (SEAT_NAME.S5 == sn) {
			init(7, 0, source, target);
		} else if (SEAT_NAME.S6 == sn) {
			init(7, 5, source, target);
		} else if (SEAT_NAME.S7 == sn) {
			init(7, 10, source, target);
		} else if (SEAT_NAME.S8 == sn) {
			init(7, 15, source, target);
		}
	}

	private void copyPlayerLayoutConvert(ChessMan[][] source, ChessMan[][] target, SEAT_NAME sn) {
		if (sn == null || source == null) {
			System.out.println("parameter error");
			return;
		}
		if (source.length != 13) {
			System.out.println("source.length!=13");
			return;
		} else if (SEAT_NAME.S1 == sn) {
			init(7, 15, source, target);
		} else if (SEAT_NAME.S2 == sn) {
			init(7, 10, source, target);
		} else if (SEAT_NAME.S3 == sn) {
			init(7, 5, source, target);
		} else if (SEAT_NAME.S4 == sn) {
			init(7, 0, source, target);
		} else if (SEAT_NAME.S5 == sn) {
			init(0, 15, source, target);
		} else if (SEAT_NAME.S6 == sn) {
			init(0, 10, source, target);
		} else if (SEAT_NAME.S7 == sn) {
			init(0, 5, source, target);
		} else if (SEAT_NAME.S8 == sn) {
			init(0, 0, source, target);
		}
	}
	private void init(int row, int col, ChessMan[][] source, ChessMan[][] target) {
		for (int i = row; i < row + 6; i++) {
			for (int j = col; j < col + 5; j++) {
				target[i][j] = new ChessMan(source[i][j].getName(), source[i][j].getTeam(), new Position(source[i][j].getPosition().row, source[i][j].getPosition().col));
			}
		}
	}
	
	/**
	 * 更改布局的视角--》旋转180度
	 * s1,s2,s3,s4更改视角，其他不用改 这个检查不在这里
	 * @param layout
	 */
	public void changeVisualAngle(ChessMan[][] layout){
		for (int i = 0; i < 7; i++) {
			for (int j = 0; j < 20; j++) {
				ChessMan temp=layout[12-i][19-j];
				layout[12-i][19-j]=layout[i][j];
				layout[i][j]=temp;
				if(layout[12-i][19-j]!=null&&layout[12-i][19-j].getName()!=NAME.EMPTY){
					layout[12-i][19-j].setPosition(new Position(12-i,19-j));
				}
				if(layout[i][j]!=null&&layout[i][j].getName()!=NAME.EMPTY){
					layout[i][j].setPosition(new Position(i,j));
				}
			}
		}
	}
	
	public void showLayout(ChessMan[][] layout){
		for (int i = 0; i < 13; i++) {
			for (int j = 0; j < 20; j++) {
				if(layout[i][j]!=null&&layout[i][j].getName()!=NAME.EMPTY){
					System.out.print(formatOut(layout[i][j].getName().name()));
				}else{
					System.out.print("          ");
				}
			}
			System.out.println();
		}
	}
	private String formatOut(String s){
		if("".equals(s)){
			return "          ";
		}
		int l=s.length();
		for(int i=0;i<10-l;i++){
			s+=" ";
		}
		return s;
	}
	
	public Position revertPosition(Position p,SEAT_NAME sn){
		if(CONSTS.SYS_CFG_CHESS_CONVERT&&(sn.equals(SEAT_NAME.S1)||sn.equals(SEAT_NAME.S2)||sn.equals(SEAT_NAME.S3)||sn.equals(SEAT_NAME.S4))){
			p.col=19-p.col;
			p.row=12-p.row;
			return p;
		}else{
			return p;
		}
	}
	
	/**
	 * 交换两个棋子
	 * 坐标一并修改
	 * @param cm1
	 * @param cm2
	 */
//	private void changeChessMan(ChessMan cm1,ChessMan cm2){
//		Position pos1=null;
//		Position pos2=null;
//		if(cm1!=null&&cm1.getName()!=NAME.EMPTY){
//			pos1=cm1.getPosition().clone();
//		}
//		if(cm2!=null&&cm2.getName()!=NAME.EMPTY){
//			pos2=cm2.getPosition().clone();
//		}
//		//
//		ChessMan temp=null;
//		if(cm1!=null){
//			temp=cm1.clone();
//		}
//		if(cm2!=null){
//			cm1=cm2.clone();
//			cm1.setPosition(pos2);
//		}else{
//			cm1=null;
//		}
//		if(temp!=null){
//			cm2=temp;
//			cm2.setPosition(pos1);
//		}
//	}
}
